package com.example.hello_world.train_Service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class Myservice02 extends Service {
    public int count=5;
    private final String s="service";
    private boolean qiut;
    public MyBinder binder= new MyBinder();

    public class MyBinder extends Binder{
        public int getCount(){
            return count;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(s,"onBind调用");
        return binder;
    }

    @Override
    public void onCreate() {
        Log.e(s,"onCreate调用");
        super.onCreate();
        //创建一个线程动态修改count的值
        new Thread(){
            @Override
            public void run() {
                while (!qiut) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(count<10){
                        count++;
                    }else{
                        stopSelf();
                    }
                    Log.i(s,"count"+count);
                }
            }
        }.start();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(s,"onUnbind调用");
        return true;
    }

    @Override
    public void onDestroy() {
        Log.e(s,"onDestroy调用");

        super.onDestroy();
        this.qiut=true;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }
}
